package org.com.system.util.model;

import org.apache.log4j.HTMLLayout;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.helpers.Transform;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;

public class MHTMLLayout extends HTMLLayout{

	 protected final int BUF_SIZE = 256;
	 protected final int MAX_CAPACITY = 1024;

	 static String TRACE_PREFIX = "<br>&nbsp;&nbsp;&nbsp;&nbsp;";

	  // output buffer appended to when format() is invoked
	  private StringBuffer sbuf = new StringBuffer(BUF_SIZE);

	  /**
	     A string constant used in naming the option for setting the the
	     location information flag.  Current value of this string
	     constant is <b>LocationInfo</b>.

	     <p>Note that all option keys are case sensitive.

	     @deprecated Options are now handled using the JavaBeans paradigm.
	     This constant is not longer needed and will be removed in the
	     <em>near</em> term.

	  */
	  public static final String LOCATION_INFO_OPTION = "LocationInfo";
	  boolean locationInfo = false;
	 String title = "系统使用情况";
	 public static final String TITLE_OPTION = "标题";
	
	 
	  public
	  void setLocationInfo(boolean flag) {
	    locationInfo = flag;
	  }

	  /**
	     Returns the current value of the <b>LocationInfo</b> option.
	   */
	  public boolean getLocationInfo() {
	    return locationInfo;
	  }

	  /**
	    The <b>Title</b> option takes a String value. This option sets the
	    document title of the generated HTML document.

	    <p>Defaults to 'Log4J Log Messages'.
	  */
	  public void setTitle(String title) {
	    this.title = title;
	  }

	  /**
	     Returns the current value of the <b>Title</b> option.
	  */
	  public String getTitle() {
	    return title;
	  }

	 /**
	     Returns the content type output by this layout, i.e "text/html".
	  */
	  public String getContentType() {
	    return "text/html";
	  }

	  /**
	     No options to activate.
	  */
	  public void activateOptions() {
	  }

	  public String format(LoggingEvent event) {

	    if(sbuf.capacity() > MAX_CAPACITY) {
	        sbuf = new StringBuffer(BUF_SIZE);
	    } else {
	        sbuf.setLength(0);
	    }

	    sbuf.append(Layout.LINE_SEP + "<tr>" + Layout.LINE_SEP);

	    sbuf.append("<td>");
	    sbuf.append(event.timeStamp - LoggingEvent.getStartTime());
	    sbuf.append("</td>" + Layout.LINE_SEP);

	    String escapedThread = Transform.escapeTags(event.getThreadName());
	    sbuf.append("<td title=\"" + escapedThread + " 线程\">");
	    sbuf.append(escapedThread);
	    sbuf.append("</td>" + Layout.LINE_SEP);

	    sbuf.append("<td title=\"级别\">");
	    if (event.getLevel().equals(Level.DEBUG)) {
	        sbuf.append("<font color=\"#339933\">");
	        sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
	        sbuf.append("</font>");
	    }else if(event.getLevel().isGreaterOrEqual(Level.WARN)) {
	        sbuf.append("<font color=\"#993300\"><strong>");
	        sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
	        sbuf.append("</strong></font>");
	    }else{
	        sbuf.append(Transform.escapeTags(String.valueOf(event.getLevel())));
	    }
	    sbuf.append("</td>" + Layout.LINE_SEP);

	    String escapedLogger = Transform.escapeTags(event.getLoggerName());
	    sbuf.append("<td title=\"" + escapedLogger + " 分类\">");
	    sbuf.append(escapedLogger);
	    sbuf.append("</td>" + Layout.LINE_SEP);

	    if(locationInfo) {
	        LocationInfo locInfo = event.getLocationInformation();
	        sbuf.append("<td>");
	        sbuf.append(Transform.escapeTags(locInfo.getFileName()));
	        sbuf.append(':');
	        sbuf.append(locInfo.getLineNumber());
	        sbuf.append("</td>" + Layout.LINE_SEP);
	    }

	    sbuf.append("<td title=\"信息\">");
	    sbuf.append(Transform.escapeTags(event.getRenderedMessage()));
	    sbuf.append("</td>" + Layout.LINE_SEP);
	    sbuf.append("</tr>" + Layout.LINE_SEP);

	    if (event.getNDC() != null) {
	        sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : xx-small;\" colspan=\"6\" title=\"嵌套诊断上下文\">");
	        sbuf.append("NDC: " + Transform.escapeTags(event.getNDC()));
	        sbuf.append("</td></tr>" + Layout.LINE_SEP);
	    }

	    String[] s = event.getThrowableStrRep();
	    if(s != null) {
	        sbuf.append("<tr><td bgcolor=\"#993300\" style=\"color:White; font-size : xx-small;\" colspan=\"6\">");
	        appendThrowableAsHTML(s, sbuf);
	        sbuf.append("</td></tr>" + Layout.LINE_SEP);
	    }

	    return sbuf.toString();
	  }

	 @SuppressWarnings("all")
	 public void appendThrowableAsHTML(String[] s, StringBuffer sbuf) {
	    if(null!=s) {
	      int len = s.length;
	      if(len == 0){
		     return;
	      }
	      sbuf.append(Transform.escapeTags(s[0]));
	      sbuf.append(Layout.LINE_SEP);
	      for(int i = 1; i < len; i++) {
		      sbuf.append(TRACE_PREFIX);
		      sbuf.append(Transform.escapeTags(s[i]));
		      sbuf.append(Layout.LINE_SEP);
	      }
	    }
	  }

	  /**
	     Returns appropriate HTML headers.
	  */
	  public String getHeader() {
	      StringBuffer sbuf = new StringBuffer();
	      sbuf.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"  + Layout.LINE_SEP);
	      sbuf.append("<html>" + Layout.LINE_SEP);
	      sbuf.append("<head>" + Layout.LINE_SEP);
	      sbuf.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
	      sbuf.append("<title>" + title + "</title>" + Layout.LINE_SEP);
	      sbuf.append("<style type=\"text/css\">"  + Layout.LINE_SEP);
	      sbuf.append("<!--"  + Layout.LINE_SEP);
	      sbuf.append("body, table {font-family: arial,sans-serif; font-size: x-small;}" + Layout.LINE_SEP);
	      sbuf.append("th {background: #336699; color: #FFFFFF; text-align: left;}" + Layout.LINE_SEP);
	      sbuf.append("-->" + Layout.LINE_SEP);
	      sbuf.append("</style>" + Layout.LINE_SEP);
	      sbuf.append("</head>" + Layout.LINE_SEP);
	      sbuf.append("<body bgcolor=\"#FFFFFF\" topmargin=\"6\" leftmargin=\"6\">" + Layout.LINE_SEP);
	      sbuf.append("<hr size=\"1\" noshade>" + Layout.LINE_SEP);
	      sbuf.append("Log session start time " + new java.util.Date() + "<br>" + Layout.LINE_SEP);
	      sbuf.append("<br>" + Layout.LINE_SEP);
	      sbuf.append("<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" bordercolor=\"#224466\" width=\"100%\">" + Layout.LINE_SEP);
	      sbuf.append("<tr>" + Layout.LINE_SEP);
	      sbuf.append("<th>时间</th>" + Layout.LINE_SEP);
	      sbuf.append("<th>线程</th>" + Layout.LINE_SEP);
	      sbuf.append("<th>级别</th>" + Layout.LINE_SEP);
	      sbuf.append("<th>分类</th>" + Layout.LINE_SEP);
	      if(locationInfo) {
	          sbuf.append("<th>File:Line</th>" + Layout.LINE_SEP);
	      }
	      sbuf.append("<th>信息</th>" + Layout.LINE_SEP);
	      sbuf.append("</tr>" + Layout.LINE_SEP);
	      return sbuf.toString();
	  }

	  /**
	     Returns the appropriate HTML footers.
	  */
	  public String getFooter() {
	      StringBuffer sbuf = new StringBuffer();
	      sbuf.append("</table>" + Layout.LINE_SEP);
	      sbuf.append("<br>" + Layout.LINE_SEP);
	      sbuf.append("</body></html>");
	      return sbuf.toString();
	  }

	  /**
	     The HTML layout handles the throwable contained in logging
	     events. Hence, this method return <code>false</code>.  
	  */
	  public boolean ignoresThrowable() {
	    return false;
	  }
	
	
}
